package org.usts.trainning.supermarket.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.usts.trainning.supermarket.service.ProCommand;

/**
 * Servlet implementation class ProviderServlet
 */
@WebServlet("/ProviderServlet")
public class ProviderServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ProviderServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request, response);
	}

	protected void processRequest(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, java.io.IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		String next = "index.jsp";
		// 代码（1）通过 if 来实现对不同请求的分发
		if (request.getParameter("command").equals("proList")) {
			ProCommand command = new ProCommand();
			next = command.findAllPro(request, response);
		} else if (request.getParameter("command").equals("lookpro")) {
			ProCommand command = new ProCommand();
			next = command.findProById(request, response);
		} else if (request.getParameter("command").equals("updatepro")) {
			ProCommand command = new ProCommand();
			next = command.updateProById(request, response);
		} else if (request.getParameter("command").equals("modifypro")) {
			ProCommand command = new ProCommand();
			next = command.modifyProById(request, response);
		} else if (request.getParameter("command").equals("deletepro")) {
			ProCommand command = new ProCommand();
			next = command.deleteProById(request, response);
		} else if (request.getParameter("command").equals("addpro")) {
			ProCommand command = new ProCommand();
			next = command.addPro(request, response);
		} else if (request.getParameter("command").equals("searchpro")) {
			ProCommand command = new ProCommand();
			next = command.searchPro(request, response);
		}
		// 代码（2）请求结果转发的视图
		dispatch(request, response, next);
	}

	protected void dispatch(HttpServletRequest request, HttpServletResponse response, String page)
			throws javax.servlet.ServletException, java.io.IOException {
		RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(page);
		dispatcher.forward(request, response);
	}

}
